package com.bjwl.display.service.safty.impl;

import com.bjwl.display.exception.LoginException;
import com.bjwl.display.exception.SysException;
import com.bjwl.display.mapper.safty.RoleMapper;
import com.bjwl.display.mapper.safty.UserMapper;
import com.bjwl.display.model.Role;
import com.bjwl.display.model.User;
import com.bjwl.display.service.safty.LoginService;
import com.bjwl.display.util.CurrUser;
import com.bjwl.display.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public CurrUser login(UserLoginVO userLoginVO) {
        boolean b=false;
        User user=null;
        Role role=null;
        Integer ro_id;
        try{
            //查询账号是否存在
             if(userMapper.queryByuId(userLoginVO.getU_id())!=null){
                 b=true;
             }
            //通过账号和密码查询
            user=userMapper.queryByuIdAnduPwd(userLoginVO);
            System.out.println("service:"+user);
            //通过用户id查询角色id
            ro_id = userMapper.queryRoIdByUserId(userLoginVO.getU_id());
            //通过角色id获取角色信息
            role = roleMapper.queryRoleByRoId(ro_id);
        }catch (Exception e){
            e.printStackTrace();
            throw new SysException("系统错误");
        }
        if(b==false){
            throw  new LoginException("账户名不存在");
        }else if(user==null){
            throw new LoginException("密码不正确");
        }
        return new CurrUser(user.getU_id(),user.getU_name(),user.getU_status(),role.getRo_id(),role.getRo_name());
    }
}
